<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="integrationCredentialForm" class="security-setting mat-padding">
  <p translate>integration.credentials</p>
  <mat-form-field style="margin-top: 16px" class="mat-block" [class.!hidden]="hideSelectType" appearance="outline">
    <mat-label translate>integration.credentials-type</mat-label>
    <mat-select required formControlName="type">
      @for (credentialsType of allowCredentialTypes; track credentialsType) {
        <mat-option
          [value]="credentialsType">
          {{ IntegrationCredentialTypeTranslation.get(credentialsType) | translate}}
        </mat-option>
      }
    </mat-select>
    @if (integrationCredentialForm.get('type').hasError('required')) {
      <mat-error translate>
        integration.credentials-type-required
      </mat-error>
    }
  </mat-form-field>
  @if (integrationCredentialForm.get('type').value === IntegrationCredentialType.Anonymous) {
    <section>
      <ng-container *ngTemplateOutlet="contentTpl"></ng-container>
    </section>
  }
  @if (integrationCredentialForm.get('type').value === IntegrationCredentialType.Basic) {
    <section>
      <div class="row flex flex-row xs:flex-col gt-xs:gap-2">
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <mat-label>{{ userNameLabel() | translate}}</mat-label>
          <input matInput formControlName="username" [required]="!userNameOptional" autocomplete="new-username">
          <tb-copy-button matSuffix
                          [class.!hidden]="!integrationCredentialForm.get('username')?.value?.length"
                          [copyText]="integrationCredentialForm.get('username')?.value">
          </tb-copy-button>
          @if (integrationCredentialForm.get('username').hasError('required')) {
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ userNameRequired() | translate }}"
                      class="tb-error">
              warning
            </mat-icon>
          }
        </mat-form-field>
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <mat-label>{{ passwordLabel() | translate}}</mat-label>
          <input matInput type="password" [required]="!passwordOptional" formControlName="password"
            autocomplete="new-password">
          <tb-toggle-password matSuffix></tb-toggle-password>
          @if (integrationCredentialForm.get('password').hasError('required')) {
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ passwordRequired() | translate }}"
                      class="tb-error">
              warning
            </mat-icon>
          }
        </mat-form-field>
      </div>
      <ng-container *ngTemplateOutlet="contentTpl"></ng-container>
    </section>
  }
  @if (integrationCredentialForm.get('type').value === IntegrationCredentialType.CertPEM) {
    <section class="dropdown-section">
      <tb-cert-upload [form]="integrationCredentialForm" [ignoreCaCert]="ignoreCaCert" [disabled]="disabled"></tb-cert-upload>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>integration.private-key-password</mat-label>
        <input matInput type="password" formControlName="privateKeyPassword">
        <tb-toggle-password matSuffix></tb-toggle-password>
      </mat-form-field>
    </section>
  }
</section>
<ng-template #contentTpl><ng-content></ng-content></ng-template>
